package dao.impl;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import dao.DocumentLevelDao;
import po.record.DocumentLevel;

public class DocumentLevelDaoImpl implements DocumentLevelDao {
	private List<DocumentLevel> leves;
	public DocumentLevelDaoImpl() {
		leves = new LinkedList<>();
	}

	@Override
	public boolean insert(DocumentLevel t) {
		if(leves.contains(t)) {
			return false;
		}
		return leves.add(t);
	}

	@Override
	public boolean update(DocumentLevel t) {
		int index = leves.indexOf(t);
		if (index == -1) {
			return false;
		} else {
			leves.set(index, t);
			return true;
		}
	}

	@Override
	public boolean delete(int id) {
		Iterator<DocumentLevel> it = leves.iterator();
		while (it.hasNext()) {
			DocumentLevel dtl = it.next();
			if (dtl.getLevelId() == id) {
				dtl.setDeleteFlag(true);
			}
		}
		return false;
	}

	@Override
	public DocumentLevel selectById(int id) {
		Iterator<DocumentLevel> dml = leves.iterator();
		while (dml.hasNext()) {
			DocumentLevel doccml = dml.next();
			if (id == doccml.getLevelId()) {
				return doccml;
			}
		}
		return null;
	}

	@Override
	public List<DocumentLevel> selectAll() {
		List<DocumentLevel> copy = new LinkedList<>();
		for (DocumentLevel dx : leves) {
			if (dx.isDeleteFlag() == false) {
				copy.add(dx);
			}
		}
		return copy;
	}

}
